package qq.android.wan.qq.db.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;

import qq.android.wan.qq.db.MyDateBaseHelper;
import qq.android.wan.qq.entity.FriendBean;
import qq.android.wan.qq.entity.SessionBean;
import qq.android.wan.qq.utils.Constant;

/**
 * Created by wxx on 2016/4/2.
 */
public class SessionDao {
    private SQLiteDatabase db;
    private MyDateBaseHelper helper;
    public SessionDao(Context context){
        helper=new MyDateBaseHelper(context);

    }
    public static void createTable(SQLiteDatabase db) {
        db.execSQL("create table " + Constant.DB_SESSION_NAME + "(" +
                "userId integer" +
                ",nick text" +
                ",lastMessage string" +
                ",unreadNum integer" +
                ",lastTime timestamp default (datetime(CURRENT_TIMESTAMP,'localtime')))");
    }

    /**
     * 将最近的聊天插入数据库(为了在第一个tab显示)
     * @param sessionBean
     */
    public void insert( SessionBean sessionBean) {
        db=helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("userId", sessionBean.getUserId());
        values.put("nick", sessionBean.getNick());
        values.put("unreadNum", sessionBean.getNick());
        values.put("lastMessage", sessionBean.getLastMessage());
        db.insert(Constant.DB_SESSION_NAME, null, values);
        db.close();
    }

    /**
     * 判断该用户是否已经添加到了数据表中
     * @param userId
     * @return
     */
    public boolean isExistByqqId(int userId) {
        db=helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from "+Constant.DB_SESSION_NAME+" where userId='" + userId+"'", null);
        boolean isExist=cursor.getCount() == 0 ? false :true;
        cursor.close();
       return isExist;
    }

    /**
     * 修改最后的聊天信息，在tab1中显示
     * @param lastMessage
     * @param userId
     */
    public void updateLastMessage(String lastMessage,int userId){
        db=helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("lastTime", "datetime(CURRENT_TIMESTAMP,'localtime')");
        values.put("lastMessage", lastMessage);
        String sql = "update "+Constant.DB_SESSION_NAME+" set lastMessage='" + lastMessage + "',lastTime=datetime(CURRENT_TIMESTAMP,'localtime') where userId=" + userId;
        db.execSQL(sql);
    }

    /**
     * 通过用户的ID在数据表中删除该信息
     * @param userId
     */
    public void deleteByqqId(int userId){
        db=helper.getWritableDatabase();
        db.delete(Constant.DB_SESSION_NAME, "userId=?", new String[]{userId + ""});
        db.close();
    }

    /**
     * 清空session表中的所有数据
     */
    public void clearAllSessionDate(){
        db=helper.getWritableDatabase();
        String sql ="delete table "+Constant.DB_SESSION_NAME;
        db.execSQL(sql);
        db.close();
    }

    /**
     * 删除session表
     */
    public void dropSession(){
        db=helper.getWritableDatabase();
        String sql ="drop table if Exists "+Constant.DB_SESSION_NAME;
        db.execSQL(sql);
        db.close();
    }

   /* *//**
     * 得到所有的聊天历史
     * @return
     *//*
    public ArrayList<SessionBean> getAllSessionList(){
        db=helper.getReadableDatabase();
        Cursor cursor=db.rawQuery("select * from "+Constant.DB_SESSION_NAME+" order by lastTime desc", null);
        if(cursor.getCount()==0){
            cursor.close();
            return new ArrayList<SessionBean>();
        }
        ArrayList<SessionBean> sessions=new ArrayList<SessionBean>();
        while(cursor.moveToNext()){
            SessionBean session=new SessionBean();
            session.setUserId(cursor.getInt(cursor.getColumnIndex("userId")));
            session.setLastMessage(cursor.getString(cursor.getColumnIndex("lastMessage")));
            session.setNick(cursor.getString(cursor.getColumnIndex("nick")));
            session.setLastTime(cursor.getString(cursor.getColumnIndex("lastTime")));
            session.setUnreadNum(cursor.getInt(cursor.getColumnIndex("unreadNum")));
            sessions.add(session);
        }
        cursor.close();
        db.close();
        return sessions;
    }*/
}
